package com.Lubaichen.Wiki.service;

import com.Lubaichen.Wiki.Pojo.Category;
import com.Lubaichen.Wiki.Pojo.CategoryExample;
import com.Lubaichen.Wiki.Pojo.Ebook;
import com.Lubaichen.Wiki.Pojo.EbookExample;
import com.Lubaichen.Wiki.mapper.CategoryMapper;

import com.Lubaichen.Wiki.req.CategoryQueryReq;
import com.Lubaichen.Wiki.req.CategorySaveReq;
import com.Lubaichen.Wiki.req.EbookSaveReq;
import com.Lubaichen.Wiki.resp.CategoryQueryResp;
import com.Lubaichen.Wiki.resp.PageResp;
import com.Lubaichen.Wiki.utils.CopyUtil;
import com.Lubaichen.Wiki.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lubaichen
 * @date 2022年03月19日 19:54
 */
@Service
public class CategoryService {
    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Autowired
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<CategoryQueryResp> all(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();// Criteria ： 相当于where条件
        categoryExample.setOrderByClause("sort asc");
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        return CopyUtil.copyList(categories, CategoryQueryResp.class);
    }


    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();// Criteria ： 相当于where条件
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        // PageHelper只对第一条sql语句生效，如果后面还有sql，那么后面这句sql就没有分页效果。
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);

        LOG.info("总页数:{}", (pageInfo.getPages()));
        LOG.info("总行数:{}", (pageInfo.getTotal()));
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(CopyUtil.copyList(categories, CategoryQueryResp.class));
        return pageResp;
    }


    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (!ObjectUtils.isEmpty(req.getId())) { // 如果传来的req有id值，那么只进行更新。
            categoryMapper.updateByPrimaryKey(category);
        } else { // 如果没有就插入
            category.setId(snowFlake.nextId()); // 雪花算法生成id
            categoryMapper.insert(category);
        }
    }


    public void deleteEbookById(Long id) {
        if (!ObjectUtils.isEmpty(id)) {
            categoryMapper.deleteByPrimaryKey(id);
        }
    }


    public List<CategoryQueryResp> search(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        return CopyUtil.copyList(categoryMapper.selectByExample(categoryExample), CategoryQueryResp.class);
    }
}
